/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pb.diplom.first.dao;

import com.pb.diplom.first.tables.CategoryJoin;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Slava
 */
public class CategoryJoinDao {

    private final String login;
    private final String password;
    private final String url;

    public CategoryJoinDao(String login, String password, String url) {
        this.login = login;
        this.password = password;
        this.url = url;
    }

    public List<CategoryJoin> getAll() {
        List<CategoryJoin> goods = new ArrayList<>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            stmt = con.prepareStatement("select t1.ID_category, t1.name_category, t2.name_category as parent from Category as t1 join Category as t2 on t2.ID_category=t1.ID_category_of_parent");
            rs = stmt.executeQuery();
            while (rs.next()) {
                CategoryJoin category = new CategoryJoin();
                category.setId(rs.getLong(1));
                category.setName(rs.getString(2));
                category.setParent(rs.getString(3));
                goods.add(category);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CommodityJoinDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    Logger.getLogger(CommodityJoinDao.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(CommodityJoinDao.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(CommodityJoinDao.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return goods;
    }
    
    public void save(CategoryJoin cat) throws SQLException {
        Connection con = null;
        Statement stmt = null;
        CallableStatement proc = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            proc = con.prepareCall(" { call insertCategory(?, ?) }");
            proc.setString(1, cat.getName());
            proc.setString(2, cat.getParent());
            proc.execute();

        } finally {
            if (stmt != null) {
                stmt.close();
            }
            if (proc != null) {
                proc.close();
            }
            if (con != null) {
                con.close();
            }
        }
    }
    
    public Long getId(CategoryJoin cat) throws SQLException {
        Long l = null;
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            //Long l;
            con = DriverManager.getConnection(url, login, password);
            stmt = con.prepareStatement("SELECT ID_category FROM Category WHERE name_category= '" + cat.getName() + "'");
            rs = stmt.executeQuery();
            while (rs.next()) {
                CategoryJoin category = new CategoryJoin();
                l = rs.getLong(1);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return l;
    }

}
